package com.think.archive.common;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultBean<T>  implements Serializable {

    private String message;
    private Integer code;

    private long timestamp;
    private T result;

    public ResultBean() {
    }

    public ResultBean(Integer code) {
        this.timestamp = System.currentTimeMillis();
        this.code = code;
    }

    private ResultBean(Integer code, String message) {
        this.timestamp = System.currentTimeMillis();
        this.message = message;
        this.code = code;
    }

    public ResultBean(Integer code, T data) {
        this.code = code;
        this.result = data;
        this.timestamp = System.currentTimeMillis();
    }

    public ResultBean(Integer code, String message, T data) {
        this.message = message;
        this.code = code;
        this.result = data;
        this.timestamp = System.currentTimeMillis();
    }


    /**
     * 版本号不正确
     *
     * @return result
     */
    public static ResultBean createResultByVersion() {
        return new ResultBean(Const.ResultCode.VERSION.getCode(), Const.ResultCode.VERSION.getValue());
    }

    /**
     * 系统异常
     *
     * @return result
     */
    public static ResultBean createResultByException() {
        return new ResultBean(Const.ResultCode.SYSTEM_EXCEPTION.getCode(), Const.ResultCode.SYSTEM_EXCEPTION.getValue());
    }


    /**
     * 操作失败
     *
     * @return result
     */
    public static ResultBean createResultByError() {
        return new ResultBean(Const.ResultCode.ERROR.getCode(), Const.ResultCode.ERROR.getValue());
    }

    /**
     * 数据异常
     *
     * @return result
     */
    public static ResultBean createResultByError(Const.ResultCode resultCode) {
        return new ResultBean(resultCode.getCode(),resultCode.getValue());
    }

    /**
     *
     * @param code
     * @return
     */
    public static ResultBean createResultByResultCode(Const.ResultCode code) {
        return new ResultBean(code.getCode(), code.getValue());
    }
    /**
     * 操作失败
     *
     * @return result
     */
    public static ResultBean createResultByError(String message) {
        return new ResultBean(Const.ResultCode.ERROR.getCode(), message);
    }

    /**
     * 操作成功
     *
     * @return data
     */
    public static <T> ResultBean createResultBySuccess(T data) {
        return new ResultBean(Const.ResultCode.SUCCESS.getCode(), Const.ResultCode.SUCCESS.getValue(),data);
    }

    /**
     * 操作
     *
     * @param code 状态码
     * @param message  信息
     * @return data
     */
    public static ResultBean createResult(Integer code, String message) {
        return new ResultBean(code, message);
    }

    /**
     * 操作
     *
     * @param code   状态码
     * @param message    信息
     * @param data 返回数据
     * @return data
     */
    public static <T> ResultBean createResult(Integer code, String message, T data) {
        return new ResultBean(code, message, data);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String toJsonString() {
        JSONObject res=new JSONObject();
        res.put("code",code);
        res.put("message",message);
        res.put("timestamp", timestamp);
        return res.toString();
    }
}
